package juc.jmm;

import juc.lock.CASLock;

import java.util.concurrent.locks.ReentrantLock;

public class Test {

    private volatile static int sum=0;

    static Object obj="";

    static ReentrantLock lock=new ReentrantLock();

    static CASLock casLock=new CASLock();

    public static void main(String[] args) {
        for (int i=0;i<10;i++){
            Thread thread=new Thread(() -> {
                //synchronized (obj){}

                /*try {
                    lock.lock();
                } finally {
                    lock.unlock();
                }*/
                for (;;){
                    if (casLock.getState()==0 && casLock.cas()){
                        try {
                            for (int j=0;j<100000;j++){
                                sum++;
                            }
                        } finally {
                            casLock.setState(0);casLock.setState(0);
                        }
                        break;
                    }
                }

            });
            thread.start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(sum);
    }

}
